package com.dingpeikeji.quickdeer.modules.rcpt.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.modules.base.web.FSysController;
import com.dingpeikeji.quickdeer.modules.rcpt.entity.TRcpt;
import com.dingpeikeji.quickdeer.modules.rcpt.entity.TUsersRcpt;
import com.dingpeikeji.quickdeer.modules.rcpt.service.TRcptService;
import com.dingpeikeji.quickdeer.modules.rcpt.service.TUsersRcptService;

/**
 * 用户最新发票信息Controller
 * @author dingpei
 * @version 2017-03-03
 */
@Controller
@RequestMapping(value = "f/usersRcpt")
public class FUsersRcptController extends BaseController {

	@Autowired
	private TUsersRcptService tUsersRcptService;
	@Autowired
	private TOrdersService ordersService;
	@Autowired
	private TRcptService tRcptService;
	
	/**
	 * 订单列表
	 */
	@RequestMapping("ordersList")
	public String orders(TUsersRcpt tUsersRcpt, HttpServletRequest request,RedirectAttributes redirectAttributes, HttpServletResponse response, Model model) {
		TOrders orders = new TOrders();
		// 当前订票人
		User user = (User) request.getSession().getAttribute(FSysController.FRONT_USER_IN_SESSION);
		orders.setOrderuserid(user.getId());
		orders.setOrderstatus(TOrders.STATUS_PAY);
		orders.setGenRcpt("no"); // 未开发票
		List<TOrders> ordersList = ordersService.findList(orders);
		redirectAttributes.addFlashAttribute("ordersList",ordersList);
		redirectAttributes.addFlashAttribute("user",user);
		return "redirect:/f/modifyUserInfo?id=fp";
	}
	
	/**
	 * 开票历史
	 */
	@RequestMapping("history")
	public String history(TUsersRcpt tUsersRcpt, HttpServletRequest request, HttpServletResponse response, Model model) {
		// 当前订票人
		User user = (User) request.getSession().getAttribute(FSysController.FRONT_USER_IN_SESSION);
		TRcpt rcpt = new TRcpt();
		rcpt.setCreateBy(user);
		Page<TRcpt> page = this.tRcptService.findPage(new Page<TRcpt>(request, response), rcpt);
		model.addAttribute("page", page);
		return "modules/rcpt/rcptHistory";
	}
	
	/**
	 * 发票申请
	 */
	@RequestMapping("genRcpt")
	public String genRcpt(String rcptIds, HttpServletRequest request, HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		// 当前订票人
		User user = (User) request.getSession().getAttribute(FSysController.FRONT_USER_IN_SESSION);
		TUsersRcpt ur = tUsersRcptService.getByUserid(user.getId());
		if(ur.getAlwaysid() == null) {
			redirectAttributes.addFlashAttribute("message", "请先填写发票信息！");
			return "redirect:/f/usersRcpt/userInfo";
		}
		this.tUsersRcptService.genRcpts(rcptIds,ur, user);
		return "redirect:/f/usersRcpt/history";
	}
	
	/**
	 * 开票信息
	 */
	@RequestMapping("userInfo")
	public String userInfo(TUsersRcpt tUsersRcpt,String rcptIds, HttpServletRequest request, Model model) {
		User user = (User) request.getSession().getAttribute(FSysController.FRONT_USER_IN_SESSION);
		tUsersRcpt = tUsersRcptService.getByUserid(user.getId());
		String outkmsfee = request.getParameter("outkmsfee");
		String outtimefee = request.getParameter("outtimefee");
		String dirverfee = request.getParameter("dirverfee");
		tUsersRcpt.setOutkmsfee(outkmsfee);
		tUsersRcpt.setOuttimefee(outtimefee);
		tUsersRcpt.setDirverfee(dirverfee);
		model.addAttribute("tUsersRcpt", tUsersRcpt);
		model.addAttribute("rcptIds", rcptIds);
		if ("提交".equals(request.getParameter("buttonname"))) {
			model.addAttribute("buttonname", "提交");//按钮显示提交还是保存
		}else{
			model.addAttribute("buttonname", "保存");//按钮显示提交还是保存
		}
		return "modules/rcpt/rcptUserInfo";
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(TUsersRcpt tUsersRcpt, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TUsersRcpt> page = tUsersRcptService.findPage(new Page<TUsersRcpt>(request, response), tUsersRcpt); 
		model.addAttribute("page", page);
		return "modules/rcpt/tUsersRcptList";
	}

	@RequestMapping(value = "save")
	public String save(TUsersRcpt tUsersRcpt,String rcptIds, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) {
		if (!beanValidator(model, tUsersRcpt)){
			return userInfo(tUsersRcpt, rcptIds, request, model);
		}
		User user = (User) request.getSession().getAttribute(FSysController.FRONT_USER_IN_SESSION);
		if(StringUtils.isBlank(tUsersRcpt.getUserid())){
			tUsersRcpt.setUserid(user.getId());
		}
		tUsersRcpt.setRcpttype("1");
		tUsersRcptService.save(tUsersRcpt);
		if(org.apache.commons.lang.StringUtils.isNotBlank(rcptIds)){
			return "redirect:/f/usersRcpt/genRcpt?rcptIds="+rcptIds;
		}
		addMessage(redirectAttributes, "保存用户发票信息成功");
		return "redirect:/f/usersRcpt/ordersList";
	}
	
	/*@RequestMapping(value = "delete")
	public String delete(TUsersRcpt tUsersRcpt, RedirectAttributes redirectAttributes) {
		tUsersRcptService.delete(tUsersRcpt);
		addMessage(redirectAttributes, "删除用户发票信息成功");
		return "redirect:"+Global.getAdminPath()+"/rcpt/tUsersRcpt/?repage";
	}*/

}